home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / ShadowMap / shadowmap.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  8.1 KB  |  281 lines

  1. //-----------------------------------------------------------------------------
  2. // File: ShadowMap.fx
  3. //
  4. // Desc: Effect file for high dynamic range cube mapping sample.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8.  
  9.  
  10. #define SMAP_SIZE 512
  11.  
  12.  
  13. #define SHADOW_EPSILON 0.00005f
  14.  
  15.  
  16. float4x4 g_mWorldView;
  17. float4x4 g_mProj;
  18. float4x4 g_mViewToLightProj;  // Transform from view space to light projection space
  19. float4   g_vMaterial;
  20. texture  g_txScene;
  21. texture  g_txShadow;
  22. float3   g_vLightPos;  // Light position in view space
  23. float3   g_vLightDir;  // Light direction in view space
  24. float4   g_vLightDiffuse = float4( 1.0f, 1.0f, 1.0f, 1.0f );  // Light diffuse color
  25. float4   g_vLightAmbient = float4( 0.3f, 0.3f, 0.3f, 1.0f );  // Use an ambient light of 0.3
  26. float    g_fCosTheta;  // Cosine of theta of the spot light
  27.  
  28.  
  29.  
  30. sampler2D g_samScene =
  31. sampler_state
  32. {
  33.     Texture = <g_txScene>;
  34.     MinFilter = Point;
  35.     MagFilter = Linear;
  36.     MipFilter = Linear;
  37. };
  38.  
  39.  
  40.  
  41.  
  42. sampler2D g_samShadow =
  43. sampler_state
  44. {
  45.     Texture = <g_txShadow>;
  46.     MinFilter = Point;
  47.     MagFilter = Point;
  48.     MipFilter = Point;
  49.     AddressU = Clamp;
  50.     AddressV = Clamp;
  51. };
  52.  
  53.  
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // Vertex Shader: VertScene
  58. // Desc: Process vertex for scene
  59. //-----------------------------------------------------------------------------
  60. void VertScene( float4 iPos : POSITION,
  61.                 float3 iNormal : NORMAL,
  62.                 float2 iTex : TEXCOORD0,
  63.                 out float4 oPos : POSITION,
  64.                 out float2 Tex : TEXCOORD0,
  65.                 out float4 vPos : TEXCOORD1,
  66.                 out float3 vNormal : TEXCOORD2,
  67.                 out float4 vPosLight : TEXCOORD3 )
  68. {
  69.     //
  70.     // Transform position to view space
  71.     //
  72.     vPos = mul( iPos, g_mWorldView );
  73.  
  74.     //
  75.     // Transform to screen coord
  76.     //
  77.     oPos = mul( vPos, g_mProj );
  78.  
  79.     //
  80.     // Compute view space normal
  81.     //
  82.     vNormal = mul( iNormal, (float3x3)g_mWorldView );
  83.  
  84.     //
  85.     // Propagate texture coord
  86.     //
  87.     Tex = iTex;
  88.  
  89.     //
  90.     // Transform the position to light projection space, or the
  91.     // projection space as if the camera is looking out from
  92.     // the spotlight.
  93.     //
  94.     vPosLight = mul( vPos, g_mViewToLightProj );
  95. }
  96.  
  97.  
  98.  
  99.  
  100. //-----------------------------------------------------------------------------
  101. // Pixel Shader: PixScene
  102. // Desc: Process pixel (do per-pixel lighting) for enabled scene
  103. //-----------------------------------------------------------------------------
  104. float4 PixScene( float2 Tex : TEXCOORD0,
  105.                  float4 vPos : TEXCOORD1,
  106.                  float3 vNormal : TEXCOORD2,
  107.                  float4 vPosLight : TEXCOORD3 ) : COLOR
  108. {
  109.     float4 Diffuse;
  110.  
  111.     // vLight is the unit vector from the light to this pixel
  112.     float3 vLight = normalize( float3( vPos - g_vLightPos ) );
  113.  
  114.     // Compute diffuse from the light
  115.     if( dot( vLight, g_vLightDir ) > g_fCosTheta ) // Light must face the pixel (within Theta)
  116.     {
  117.         // Pixel is in lit area. Find out if it's
  118.         // in shadow using 2x2 percentage closest filtering
  119.  
  120.         //transform from RT space to texture space.
  121.         float2 ShadowTexC = 0.5 * vPosLight.xy / vPosLight.w + float2( 0.5, 0.5 );
  122.         ShadowTexC.y = 1.0f - ShadowTexC.y;
  123.  
  124.         // transform to texel space
  125.         float2 texelpos = SMAP_SIZE * ShadowTexC;
  126.         
  127.         // Determine the lerp amounts           
  128.         float2 lerps = frac( texelpos );
  129.  
  130.         //read in bilerp stamp, doing the shadow checks
  131.         float sourcevals[4];
  132.         sourcevals[0] = (tex2D( g_samShadow, ShadowTexC ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;  
  133.         sourcevals[1] = (tex2D( g_samShadow, ShadowTexC + float2(1.0/SMAP_SIZE, 0) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;  
  134.         sourcevals[2] = (tex2D( g_samShadow, ShadowTexC + float2(0, 1.0/SMAP_SIZE) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;  
  135.         sourcevals[3] = (tex2D( g_samShadow, ShadowTexC + float2(1.0/SMAP_SIZE, 1.0/SMAP_SIZE) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;  
  136.         
  137.         // lerp between the shadow values to calculate our light amount
  138.         float LightAmount = lerp( lerp( sourcevals[0], sourcevals[1], lerps.x ),
  139.                                   lerp( sourcevals[2], sourcevals[3], lerps.x ),
  140.                                   lerps.y );
  141.         // Light it
  142.         Diffuse = ( saturate( dot( -vLight, normalize( vNormal ) ) ) * LightAmount * ( 1 - g_vLightAmbient ) + g_vLightAmbient )
  143.                   * g_vMaterial;
  144.     } else
  145.     {
  146.         Diffuse = g_vLightAmbient * g_vMaterial;
  147.     }
  148.  
  149.     return tex2D( g_samScene, Tex ) * Diffuse;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. //-----------------------------------------------------------------------------
  156. // Vertex Shader: VertLight
  157. // Desc: Process vertex for the light object
  158. //-----------------------------------------------------------------------------
  159. void VertLight( float4 iPos : POSITION,
  160.                 float3 iNormal : NORMAL,
  161.                 float2 iTex : TEXCOORD0,
  162.                 out float4 oPos : POSITION,
  163.                 out float2 Tex : TEXCOORD0 )
  164. {
  165.     //
  166.     // Transform position to view space
  167.     //
  168.     oPos = mul( iPos, g_mWorldView );
  169.  
  170.     //
  171.     // Transform to screen coord
  172.     //
  173.     oPos = mul( oPos, g_mProj );
  174.  
  175.     //
  176.     // Propagate texture coord
  177.     //
  178.     Tex = iTex;
  179. }
  180.  
  181.  
  182.  
  183.  
  184. //-----------------------------------------------------------------------------
  185. // Pixel Shader: PixLight
  186. // Desc: Process pixel for the light object
  187. //-----------------------------------------------------------------------------
  188. float4 PixLight( float2 Tex : TEXCOORD0,
  189.                  float4 vPos : TEXCOORD1 ) : COLOR
  190. {
  191.     return tex2D( g_samScene, Tex );
  192. }
  193.  
  194.  
  195.  
  196.  
  197. //-----------------------------------------------------------------------------
  198. // Vertex Shader: VertShadow
  199. // Desc: Process vertex for the shadow map
  200. //-----------------------------------------------------------------------------
  201. void VertShadow( float4 Pos : POSITION,
  202.                  float3 Normal : NORMAL,
  203.                  out float4 oPos : POSITION,
  204.                  out float2 Depth : TEXCOORD0 )
  205. {
  206.     //
  207.     // Compute the projected coordinates
  208.     //
  209.     oPos = mul( Pos, g_mWorldView );
  210.     oPos = mul( oPos, g_mProj );
  211.  
  212.     //
  213.     // Store z and w in our spare texcoord
  214.     //
  215.     Depth.xy = oPos.zw;
  216. }
  217.  
  218.  
  219.  
  220.  
  221. //-----------------------------------------------------------------------------
  222. // Pixel Shader: PixShadow
  223. // Desc: Process pixel for the shadow map
  224. //-----------------------------------------------------------------------------
  225. void PixShadow( float2 Depth : TEXCOORD0,
  226.                 out float4 Color : COLOR )
  227. {
  228.     //
  229.     // Depth is z / w
  230.     //
  231.     Color = Depth.x / Depth.y;
  232. }
  233.  
  234.  
  235.  
  236.  
  237. //-----------------------------------------------------------------------------
  238. // Technique: RenderScene
  239. // Desc: Renders scene objects
  240. //-----------------------------------------------------------------------------
  241. technique RenderScene
  242. {
  243.     pass p0
  244.     {
  245.         VertexShader = compile vs_1_1 VertScene();
  246.         PixelShader = compile ps_2_0 PixScene();
  247.     }
  248. }
  249.  
  250.  
  251.  
  252.  
  253. //-----------------------------------------------------------------------------
  254. // Technique: RenderLight
  255. // Desc: Renders the light object
  256. //-----------------------------------------------------------------------------
  257. technique RenderLight
  258. {
  259.     pass p0
  260.     {
  261.         VertexShader = compile vs_1_1 VertLight();
  262.         PixelShader = compile ps_1_1 PixLight();
  263.     }
  264. }
  265.  
  266.  
  267.  
  268.  
  269. //-----------------------------------------------------------------------------
  270. // Technique: RenderShadow
  271. // Desc: Renders the shadow map
  272. //-----------------------------------------------------------------------------
  273. technique RenderShadow
  274. {
  275.     pass p0
  276.     {
  277.         VertexShader = compile vs_1_1 VertShadow();
  278.         PixelShader = compile ps_2_0 PixShadow();
  279.     }
  280. }
  281.